package com.amigo.online.provider.video.util;

import java.io.IOException;

import cieloecommerce.sdk.Merchant;
import cieloecommerce.sdk.ecommerce.CieloEcommerce;
import cieloecommerce.sdk.ecommerce.Payment;
import cieloecommerce.sdk.ecommerce.Sale;
import cieloecommerce.sdk.ecommerce.SaleResponse;
import cieloecommerce.sdk.ecommerce.request.CieloRequestException;

public class PayUtil {
    /**
     * 返回值可能为null,表示失败。status为2时支付成功 .文档 https://github.com/DeveloperCielo/API-3.0-Java
     *
     * @param order_num
     * @param pay_money
     * @param pin
     * @param cards
     * @param expiration_date
     * @param card_number
     * @param holder
     * @return
     * @throws IOException
     * @throws CieloRequestException return
     */
    public static SaleResponse creditCardPay(String order_num, Integer pay_money, String pin, String cards,
                                             String expiration_date, String card_number, String holder) throws IOException, CieloRequestException {
        Merchant merchant = new Merchant("fb99a3c3-742c-4652-a423-94f4c5d7780f",
                "xdQODFVhiwAL82xOzfElgKTwb1aRaWDOdNfd6ZQr");
        Sale sale = new Sale(order_num); // 订单号
        sale.customer("PROMOTE ORDER");
        Payment payment = sale.payment(pay_money); // 单位是分
        payment.creditCard(pin, cards).setExpirationDate(expiration_date).setCardNumber(card_number).setHolder(holder);

        sale = new CieloEcommerce(merchant, cieloecommerce.sdk.ecommerce.Environment.PRODUCTION).createSale(sale);
        String paymentId = sale.getPayment().getPaymentId();
        if (paymentId != null && !paymentId.equals("")) {
            SaleResponse response = new CieloEcommerce(merchant, cieloecommerce.sdk.ecommerce.Environment.PRODUCTION)
                    .captureSale(paymentId, pay_money, 0);
            return response;
        }
        return null;

    }
}
